import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";
import _filter from "lodash/filter";
var _excluded = ["fileFormat", "contentType"];

var _dec, _dec2, _dec3, _class, _class2;

import React, { PureComponent } from 'react';
import { Form, Input, Modal, Select, Spin } from 'antd';
import { Bind } from 'lodash-decorators';
import intl from 'utils/intl';
var FormItem = Form.Item;
var Option = Select.Option;
var formLayout = {
  labelCol: {
    span: 6
  },
  wrapperCol: {
    span: 16
  }
};
/**
 * 新建或编辑模态框数据展示
 * @extends {PureComponent} - React.PureComponent
 * @reactProps {Function} onAdd - 添加确定的回调函数
 * @reactProps {Function} onEdit - 编辑确定的回调函数
 * @reactProps {Object} fileDetail - 表格中信息的一条记录
 * @reactProps {Boolean} isCreate - 是否为新建账户
 * @reactProps {String} anchor - 模态框弹出方向
 * @return React.element
 */

var Drawer = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(Drawer, _PureComponent);

  var _super = _createSuper(Drawer);

  function Drawer() {
    _classCallCheck(this, Drawer);

    return _super.apply(this, arguments);
  }

  _createClass(Drawer, [{
    key: "onOk",
    value: function onOk() {
      var _this$props = this.props,
          form = _this$props.form,
          onAdd = _this$props.onAdd,
          isCreate = _this$props.isCreate,
          fileDetail = _this$props.fileDetail,
          onEdit = _this$props.onEdit;
      var uploadConfigId = fileDetail.uploadConfigId,
          objectVersionNumber = fileDetail.objectVersionNumber,
          _token = fileDetail._token;
      form.validateFields(function (err, values) {
        if (_isEmpty(err)) {
          var fileFormat = values.fileFormat,
              contentType = values.contentType,
              others = _objectWithoutProperties(values, _excluded);

          if (isCreate) {
            onAdd(_objectSpread({
              fileFormat: fileFormat.join(','),
              contentType: contentType.join(',')
            }, others));
          } else {
            onEdit(_objectSpread({
              _token: _token,
              uploadConfigId: uploadConfigId,
              objectVersionNumber: objectVersionNumber,
              fileFormat: fileFormat.join(','),
              contentType: contentType.join(',')
            }, others));
          }
        }
      });
    }
    /**
     *  改变文件分类
     */

  }, {
    key: "changeContentType",
    value: function changeContentType(value) {
      var _this$props2 = this.props,
          _this$props2$fileForm = _this$props2.fileFormatsList,
          fileFormatsList = _this$props2$fileForm === void 0 ? [] : _this$props2$fileForm,
          setFieldsValue = _this$props2.form.setFieldsValue,
          onChangeFileFormats = _this$props2.onChangeFileFormats;

      var newFileFormat = _filter(fileFormatsList, function (item) {
        return value.indexOf(item.parentValue) >= 0;
      });

      onChangeFileFormats(newFileFormat);
      setFieldsValue({
        fileFormat: []
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          visible = _this$props3.visible,
          onCancel = _this$props3.onCancel,
          saving = _this$props3.saving,
          anchor = _this$props3.anchor,
          fileDetail = _this$props3.fileDetail,
          isCreate = _this$props3.isCreate,
          _this$props3$fileType = _this$props3.fileTypeList,
          fileTypeList = _this$props3$fileType === void 0 ? [] : _this$props3$fileType,
          _this$props3$fileUnit = _this$props3.fileUnitList,
          fileUnitList = _this$props3$fileUnit === void 0 ? [] : _this$props3$fileUnit,
          _this$props3$newFileF = _this$props3.newFileFormat,
          newFileFormat = _this$props3$newFileF === void 0 ? [] : _this$props3$newFileF,
          _this$props3$detailLo = _this$props3.detailLoading,
          detailLoading = _this$props3$detailLo === void 0 ? false : _this$props3$detailLo;
      var bucketName = fileDetail.bucketName;
      var getFieldDecorator = this.props.form.getFieldDecorator;
      var fileMaxUnitSelector = getFieldDecorator('storageUnit', {
        initialValue: fileDetail.storageUnit ? fileDetail.storageUnit : 'MB'
      })( /*#__PURE__*/React.createElement(Select, {
        style: {
          width: 65
        }
      }, fileUnitList && fileUnitList.map(function (item) {
        return /*#__PURE__*/React.createElement(Option, {
          value: item.value,
          key: item.value
        }, item.meaning);
      })));
      return /*#__PURE__*/React.createElement(Modal, {
        destroyOnClose: true,
        width: 520,
        title: isCreate ? intl.get('cfile.fileUpload.view.message.create').d('新建文件上传详细配置') : intl.get('cfile.fileUpload.view.message.edit').d('编辑文件上传详细配置'),
        visible: visible,
        onCancel: onCancel,
        onOk: this.onOk,
        confirmLoading: saving,
        wrapClassName: "ant-modal-sidebar-".concat(anchor),
        transitionName: "move-".concat(anchor)
      }, /*#__PURE__*/React.createElement(Spin, {
        spinning: detailLoading
      }, /*#__PURE__*/React.createElement(Form, null, /*#__PURE__*/React.createElement(FormItem, Object.assign({
        label: intl.get('cfile.fileUpload.model.fileUpload.bucketName').d('分组')
      }, formLayout), getFieldDecorator('bucketName', {
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cfile.fileUpload.model.fileUpload.bucketName').d('分组')
          })
        }, {
          pattern: /^[a-z0-9-]*$/,
          message: intl.get('cfile.fileUpload.view.validation.bucketName').d('只能由小写字母、数字，"-"组成')
        }],
        initialValue: bucketName
      })( /*#__PURE__*/React.createElement(Input, {
        disabled: !isCreate,
        trim: true,
        typeCase: "lower",
        inputChinese: false
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({
        label: intl.get('cfile.fileUpload.model.fileUpload.directory').d('上传目录')
      }, formLayout), getFieldDecorator('directory', {
        initialValue: fileDetail ? fileDetail.directory : ''
      })( /*#__PURE__*/React.createElement(Input, {
        disabled: !isCreate
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({
        label: intl.get('cfile.fileUpload.model.fileUpload.contentType').d('文件分类')
      }, formLayout), getFieldDecorator('contentType', {
        rules: [{
          required: false,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cfile.fileUpload.model.fileUpload.contentType').d('文件分类'),
            type: 'array'
          })
        }],
        initialValue: _isEmpty(fileDetail.contentType) ? [] : fileDetail.contentType.split(',')
      })( /*#__PURE__*/React.createElement(Select, {
        allowClear: true,
        onChange: this.changeContentType,
        mode: "multiple"
      }, fileTypeList && fileTypeList.map(function (item) {
        return /*#__PURE__*/React.createElement(Option, {
          value: item.value,
          key: item.value
        }, item.meaning);
      })))), /*#__PURE__*/React.createElement(FormItem, Object.assign({
        label: intl.get('cfile.fileUpload.model.fileUpload.fileFormat').d('文件格式')
      }, formLayout), getFieldDecorator('fileFormat', {
        rules: [{
          required: false,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cfile.fileUpload.model.fileUpload.fileFormat').d('文件格式'),
            type: 'array'
          })
        }],
        initialValue: _isEmpty(fileDetail.fileFormat) ? [] : fileDetail.fileFormat.split(',')
      })( /*#__PURE__*/React.createElement(Select, {
        allowClear: true,
        mode: "multiple"
      }, newFileFormat && newFileFormat.map(function (item) {
        return /*#__PURE__*/React.createElement(Option, {
          value: item.value,
          key: item.value
        }, item.meaning);
      })))), /*#__PURE__*/React.createElement(FormItem, Object.assign({
        label: intl.get('cfile.fileUpload.model.fileUpload.storageSize').d('文件大小限制')
      }, formLayout), getFieldDecorator('storageSize', {
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cfile.fileUpload.model.fileUpload.storageSize').d('文件大小限制')
          })
        }, {
          min: 0,
          pattern: /^\d+$/,
          message: intl.get('cfile.fileAggregate.view.message.patternValidate').d('请输入大于等于0的整数')
        }],
        initialValue: fileDetail ? fileDetail.storageSize : ''
      })( /*#__PURE__*/React.createElement(Input, {
        type: "number",
        addonAfter: fileMaxUnitSelector
      }))))));
    }
  }]);

  return Drawer;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "onOk", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "onOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "changeContentType", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "changeContentType"), _class2.prototype)), _class2)) || _class);
export { Drawer as default };